* Table A11
********************************************************************************
*Main data pathway globals, set directory to clean data folder
********************************************************************************
global cleandata "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"
cd "$cleandata"
global rawdata "S:\Project\DemoSos2\data2020\"
capture mkdir "$cleandata"
global results "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\ResultsRev\"


********************************************************************************
*Determine last year of GPs who terminate
*For stayers vs leavers
*	ID gps with any exog swap
*	ID gps with terminations
*	For gps with no terminates (no move, no last year)
*		Compare patients with exog swap vs stayers (including those who endog swap)
********************************************************************************
*Determine years GP ends work or moves muni
{
*First determine which GP each patient has at start of year
use "$rawdata\KUHR\PasientLister.dta", clear
rename opphoeraarsak oppharsak
drop if BehandlerID == . | BehandlerID == 0 |  lopenr == "0" | lopenr == "" //PasidKUHR == . | PasidKUHR == 0 |
save "term_temp2.dta", replace
forvalues i = 2001/2020     {
	use "term_temp2.dta", clear
	keep if tomdato > d(01jan`i')
	gen year = `i'
	gen swap = 1 if fomdato<= d(01jan`i') & tomdato <= d(31dec`i')
	replace swap = 0 if swap==.
	gen d_swap = tomdato if swap==1
	sort lopenr fomdato
	gen r_swap = opprettelseaarsak[_n+1] if swap==1
	drop if fomdato > d(01jan`i')
	rename BehandlerID gpid
	keep  lopenr year gpid swap r_swap d_swap   
	save term_newtemp`i', replace

}
use term_newtemp2001, clear
forvalues i = 2002/2020       {
append using term_newtemp`i'
}
*Second collapse to get GP-by-year dataset, determine last year in data
gen patient_count=1
collapse (count) patient_count, by(gpid year)
sort gpid year
by gpid: egen last_year=max(year)
replace last_year=. if year!=last_year
replace last_year=1 if last_year!=.

*Now deal with moves
gen aar=year
gen BehandlerID=gpid
merge 1:m aar BehandlerID using "$rawdata\KUHR\FastLegeInfo.dta", keepusing(kommunenr)
drop if _merge==2
drop aar BehandlerID _merge
*Determine move dates
gen kommunenr_missing=1 if kommunenr==.
replace kommunenr=kommunenr[_n-1] if kommunenr==.
duplicates tag gpid year, gen(dupe_gp_muni)
bysort gpid: egen dupe_gp_muni_max=max(dupe_gp_muni)
bysort gpid year: egen maxmuni=max(kommunenr)
bysort gpid year: egen minmuni=min(kommunenr)

*First do one obs per gp year
gen move_nextyr=.
replace move_nextyr=1 if dupe_gp_muni==0 & gpid==gpid[_n+1] & kommunenr!=kommunenr[_n+1] ///
	& dupe_gp_muni[_n+1]==0 & kommunenr!=. //this year and next year only 1 muni
replace move_nextyr=1 if dupe_gp_muni==0 & gpid==gpid[_n+1] ///
	& (maxmuni!=maxmuni[_n+1] & minmuni!=minmuni[_n+1]) ///
	& dupe_gp_muni[_n+1]==1 & kommunenr!=. //next year has 2 muni
*Second do 2 obs per gp year, drop if a muni stops showing, even if other muni consistent
replace move_nextyr=1 if dupe_gp_muni==1 & gpid==gpid[_n+1]  & ///
	(maxmuni!=maxmuni[_n+1] | minmuni!=minmuni[_n+1])
*Last do 3 obs per gp per year (only 15 cases, will clean manually)
replace move_nextyr=1 if dupe_gp_muni==2 //both instances had at least 1 list endog

*Collapse to gp-year level, keep relevant years
collapse last_year move_nextyr dupe_gp_muni, by(gpid year)
drop if year>2015
	
*Label
la var gpid "GP in beg. of year"
la var last_year "Final year GP in data"
la var move_nextyr "GP terminates a list this year but not last year of data"
la var dupe_gp_muni "Number of muni GP has in data"
*Save 
save GP_terminate_moveVSlastyr.dta, replace
}

*Determine GPs with any exog swaps, merge on terminations
{
use  clean_patientlevel_file_any_rev3.dta, clear
keep if yr_str_exog_swap!=.
keep lopenr gpid* red_term_swap* yr_str_exog_swap
reshape long gpid red_term_swap, i(lopenr) j(year)
keep if year == yr_str_exog_swap
keep if yr_str_exog_swap<=2014 & yr_str_exog_swap>=2005
collapse (max) red_term_swap, by(gpid year)
merge 1:1 gpid year using "GP_terminate_moveVSlastyr.dta"

gen reduction=1 if red_term_swap==1 & (last_year!=1 & move_nextyr!=1)
gen termination=1 if red_term_swap==1 & (last_year==1 | move_nextyr==1)
keep year gpid reduction termination last_year move_nextyr dupe_gp_muni
*Save 
save GP_terminateVSreduction.dta, replace
}

*Now get list of all patients, merge on exog swap year, and type
{
*First need patient year dataset
use "$rawdata\KUHR\PasientLister.dta", clear
rename opphoeraarsak oppharsak
drop if BehandlerID == . | BehandlerID == 0 |  lopenr == "0" | lopenr == "" //PasidKUHR == . | PasidKUHR == 0 |
save "allpt_temp2.dta", replace
forvalues i = 2001/2020     {
	use "allpt_temp2.dta", clear
	keep if tomdato > d(01jan`i')
	gen year = `i'
	gen swap = 1 if fomdato<= d(01jan`i') & tomdato <= d(31dec`i')
	replace swap = 0 if swap==.
	gen d_swap = tomdato if swap==1
	sort lopenr fomdato
	gen r_swap = opprettelseaarsak[_n+1] if swap==1
	drop if fomdato > d(01jan`i')
	rename BehandlerID gpid
	keep  lopenr year gpid swap r_swap d_swap   
	save allpt_temp`i', replace

}
use allpt_temp2001, clear
forvalues i = 2002/2020       {
append using allpt_temp`i'
}
gen red_term_swap = 1 if swap==1 & r_swap=="Lege har redusert eller avsluttet praksis" 
merge m:1 gpid year using "GP_terminateVSreduction.dta"
keep if (reduction==1 | termination==1)
save Patient_terminateVSreduction, replace
}

*Merge on chars for balance
{
use Patient_terminateVSreduction, clear
gen aar=year-2

*College 2 years before 
merge m:1 lopenr aar using "Uutdanning.dta", keepusing(BU) keep(1 3) nogen
gen bu_1 = real(substr(BU, 1, 1))
gen college0 = inlist(bu_1,6,7,8) if bu_1!=. & bu_1!=9
label var college0 "Some college 2 yrs before swap"
drop BU bu_1

*Married 2 years before
merge m:1 lopenr aar using "sivilstand.dta", keepusing(sivilstand) keep(1 3) nogen
ge married_any= sivilstand==2|sivilstand==6 if sivilstand!=.
label var married_any "Married/Partnership 2 years before swap"
drop sivilstand 

*Country of orgin, birth year, sex
merge m:1 lopenr using "$rawdata\Befolkn\Demogr\faste_oppl.dta", ///
	keepusing(invkat foedselsaar kjoenn ) keep(1 3) nogen
rename (foedselsaar kjoenn) (byr sex)
gen NORborn = (invkat=="A")
gen age=year-byr
drop invkat 

*Children
merge m:1 lopenr using "$rawdata\Befolkn\Familie\slekt.dta", ///
	keepusing(antbarn) keep(1 3) gen(merge_children)
replace antbarn=0 if antbarn==.

*Income and public programs
rename (year aar) (year_hold year)
merge m:1 lopenr year using income_datanew, ///
	keep(1 3) nogen keepusing(tot_income lab_income woverfor)
rename (year_hold year) (year aar)
rename tot_income tot_income_any
ge dwoverfor_any = woverfor >0 if woverfor!=.	
drop woverfor lab_income

*GP visits
rename (year aar) (year_hold year)
merge m:1 lopenr year using khurh_annual, keep(1 3) nogen keepusing(total reimburse)
rename (year_hold year) (year aar)
replace total=0 if total==. 
gen total_any=(total>0)
rename total dtotal_any
replace reimburse=0 if reimburse==.
merge m:1 year using cpi, nogen
ge reimburse_def = reimburse*cpi/100
drop cpi reimburse
rename reimburse_def reimburse
rename reimburse reimburse_any


*Chronic conditions
rename (year aar) (year_hold year)
merge m:1 lopenr year using "khurh_annual", keep(1 3) nogen ///
	keepusing(cancer digest_chronic heart_chronic endoc_chronic any_cardiogp total ///
	simple_contact major_surg life_style_adv reimburse labtest procedure)
rename (year_hold year) (year aar)
* khur only from 2006
keep if year>=2008
for var cancer digest_chronic heart_chronic endoc_chronic any_cardiogp: ///
	rename X X_base
foreach var in cancer digest_chronic heart_chronic endoc_chronic any_cardiogp  {
	replace `var'_base = 0 if `var'_base==. 
	ge d`var'_base = `var'_base> 0 
}
egen any_chronic0 = rmax(dcancer_base ddigest_chronic_base dheart_chronic_base ///
	dendoc_chronic_base dany_cardiogp_base) 	
drop total simple_contact procedure life_style_adv major_surg labtest reimburse ///
	cancer digest_chronic heart_chronic endoc_chronic any_cardiogp
rename 	any_chronic0 any_chronic_any0
	
*Hospital
rename (year aar) (year_hold year)
merge m:1 lopenr year using npr, keep(1 3) nogen keepusing(npr_days)
rename (year_hold year) (year aar)
gen dnpr_any=(npr_days>0) if year>=2010

*Hours
merge m:1 lopenr year using hours, keep(1 3) nogen keepusing(hours)

*Sick leave
merge m:1 lopenr year using sick_leave, keep(1 3) nogen keepusing(total_days_sl)
replace total_days_sl=0 if total_days_sl==.
rename total_days_sl total_days_sl_any

label var college0			"College Degree"
label var married_any		"Married"
label var NORborn			"Born in Norway"
label var antbarn			"# of Children"
label var tot_income_any	"Total Income"
label var hours				"Hours"
label var dwoverfor_any		"Any Welfare Benefits"
label var dtotal_any		"Any Visits"
label var total_any			"Nb. Visits"
label var any_chronic_any0	"Chron Cond"
label var reimburse_any		"Reimbursements"
label var dnpr_any			"Hospital"
label var total_days_sl_any	"Sick Leave Days"

save Patient_terminateVSreduction2, replace
}


*Analysis, stayers vs leavers for reduction
{
use Patient_terminateVSreduction2, clear

label var college0			"College Degree"
label var married_any		"Married"
label var NORborn			"Born in Norway"
label var antbarn			"# of Children"
label var tot_income_any	"Total Income"
label var hours				"Hours"
label var dwoverfor_any		"Any Welfare Benefits"
label var total_any			"Any Visits"
label var dtotal_any		"Nb. Visits"
label var any_chronic_any0	"Chron Cond"
label var reimburse_any		"Reimbursements"
label var dnpr_any			"Hospital"
label var total_days_sl_any	"Sick Leave Days"

destring sex, replace
keep if age>=55 //focus on our sample
bysort gpid year: egen gp_red_term=max(red_term_swap)
by gpid year: egen gp_red_term_ct=sum(red_term_swap)

keep if reduction==1

replace red_term_swap=0 if red_term_swap==.
global baseline "college0 married_any NORborn antbarn tot_income_any hours dwoverfor_any "
global baseline "total_any dtotal_any any_chronic_any0 reimburse_any dnpr_any"
global controls "i.yr_swap i.swap_age male" 
global controls2 "i.year i.age sex" 

*Run balance test with previous GP FE and controls, standardized, append


*Restirct between 10 and 1000 reduction, All controls (year, age, sex)
eststo clear
foreach var in $baseline {
eststo `var':  areg `var' red_term_swap $controls2 ///
	if gp_red_term_ct>=10 & gp_red_term_ct<=1000 ///
	,  a(gpid) cl(gpid) //$controls2 a(swap_GPIDprev) cl(swap_GPIDnew)  
estadd ysumm
}
esttab  , keep(red_term_swap) label stats(ymean N, fmt(%9.3g)) ///
	b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)
esttab using "$results/ExternalValidity_10to1000Leavers_AllControl.tex", replace ///
	keep(red_term_swap) label stats(ymean N, fmt(%9.3g)) ///
	b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)
esttab using "$results/ExternalValidity_10to1000Leavers_AllControl.txt", replace ///
	keep(red_term_swap) label stats(ymean N, fmt(%9.3g)) ///
	b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)
}

